package com.fq.modules.sys.menu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fq.api.api.ApiRest;
import com.fq.api.api.controller.BaseController;
import com.fq.api.api.dto.ListSortReqDTO;
import com.fq.modules.sys.menu.dto.QuerySysMenuDTO;
import com.fq.modules.sys.menu.dto.SysMenuDTO;
import com.fq.modules.sys.menu.entity.SysMenu;
import com.fq.modules.sys.menu.service.SysMenuService;
import com.fq.modules.sys.menu.vo.MenuRouteVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 超chao
 * @Description 系统菜单控制器
 * @Date 2024/11/21/周四 10:20
 * @Version 1.0
 */
@Api(tags={"系统菜单"})
@RestController
@RequestMapping("/api/sys/menu")
public class SysMenuController extends BaseController {

    @Autowired
    private SysMenuService baseService;

    @ApiOperation(value = "查询--用户个人菜单列表")
    @RequestMapping(value = "/routes", method = { RequestMethod.GET})
    public ApiRest<List<MenuRouteVO>> routes(@RequestParam(value = "userId") Long userId) {
        List<MenuRouteVO> list = baseService.listRoutes(userId);
        return super.success(list);
    }

    @RequiresPermissions(value = {"sys:menu:add", "sys:menu:update"}, logical = Logical.OR)
    @ApiOperation(value = "增改--菜单信息")
    @RequestMapping(value = "/save", method = { RequestMethod.POST})
    public ApiRest<Void> save(@Validated @RequestBody SysMenuDTO reqDTO) {
        baseService.save(reqDTO);
        return super.success();
    }

    @RequiresPermissions(value = {"sys:menu:delete"})
    @ApiOperation(value = "批量删除--菜单信息")
    @RequestMapping(value = "/delete", method = { RequestMethod.DELETE})
    public ApiRest<Void> edit(@RequestParam("ids") List<Long> ids) {
        //根据ID删除
        baseService.delete(ids);
        return super.success();
    }

    @ApiOperation(value = "查询--菜单详情")
    @RequestMapping(value = "/detail", method = { RequestMethod.GET})
    public ApiRest<SysMenuDTO> find(@RequestParam(value = "id") Long id) {
        SysMenu entity = baseService.getById(id);
        SysMenuDTO dto = new SysMenuDTO();
        BeanUtils.copyProperties(entity, dto);
        return super.success(dto);
    }

    @RequiresPermissions(value = {"sys:menu:list"})
    @ApiOperation(value = "分页查询--菜单列表")
    @RequestMapping(value = "/paging", method = { RequestMethod.GET})
    public ApiRest<IPage<SysMenuDTO>> paging(@Validated @ModelAttribute QuerySysMenuDTO reqDTO) {

        return super.success(baseService.paging(reqDTO));
    }

    @ApiOperation(value = "分页查询--菜单树结构")
    @RequestMapping(value = "/tree", method = { RequestMethod.GET})
    public ApiRest<List<MenuRouteVO>> tree() {

        //分页查询并转换
        List<MenuRouteVO> list = baseService.listTree();

        return super.success(list);
    }

    @RequiresPermissions(value = {"sys:menu:sort"})
    @ApiOperation(value = "操作--菜单排序")
    @RequestMapping(value = "/sort", method = { RequestMethod.PUT})
    public ApiRest<Void> sort(@RequestBody ListSortReqDTO reqDTO) {
        baseService.sort(reqDTO.getId(), reqDTO.getSort());
        return super.success();
    }
    @RequiresPermissions(value = {"sys:menu:state"})
    @ApiOperation(value = "操作--禁用启用菜单")
    @RequestMapping(value = "/change-state", method = { RequestMethod.PUT})
    public ApiRest<Void> changeState(@Validated @RequestBody com.fq.common.core.api.dto.BaseChangeStateDTO<Long> reqDTO) {
        baseService.changeState(reqDTO);
        return super.success();
    }

}
